<?php

namespace App\Http\Middleware;

use Closure;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Log;

class RememberUrlPrevForAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (!Admin::guard()->check()) {
            $current_url = url()->current();
            
            $pregs = [
                '/\/admin$/',
            ];
            // dd(preg_match('/\/gteam\/\d/', 'http://smartgh.test/gteam/87/add_member?from_tag=attend'));
            // $remenber_urls = ['/competition', '/activity', '/signs', '/lottery', '/aquestionnaire', '/vote', '/users', '/association_activity'];
            // $count = collect($pregs)->filter(function($preg) use ($current_url) {
            //     return preg_match($preg, $current_url) == 1;
            // })->count();
            $redirectToSessionKey = 'admin_redirect_to';
            // if ($count > 0) {
                Cookie::queue($redirectToSessionKey,$current_url,5);
            // }
        }
        return $next($request);
    }
}
